
package Main;

import java.awt.HeadlessException;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;

/**
 *
 * @author anas
 */
public class Supprimer extends javax.swing.JInternalFrame {

    Connection conn= null;
    ResultSet rs = null;
    PreparedStatement ps =null;
    
    public Supprimer() {
        initComponents();
        conn =gestion_chambes_hotel.Gestion_Chambes_Hotel.DB_Connection();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cin = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        Supprimer_Client = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setMinimumSize(new java.awt.Dimension(894, 555));
        setPreferredSize(new java.awt.Dimension(894, 555));
        getContentPane().setLayout(null);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/Delete.png"))); // NOI18N
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel1MouseReleased(evt);
            }
        });
        jLabel1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jLabel1KeyPressed(evt);
            }
        });
        getContentPane().add(jLabel1);
        jLabel1.setBounds(360, 0, 140, 50);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/Annuler.png"))); // NOI18N
        jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel2MouseReleased(evt);
            }
        });
        getContentPane().add(jLabel2);
        jLabel2.setBounds(510, 0, 140, 50);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/Delete_all.png"))); // NOI18N
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel3MouseReleased(evt);
            }
        });
        getContentPane().add(jLabel3);
        jLabel3.setBounds(660, 0, 150, 50);

        cin.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        cin.setText("Entrer le CIN ...");
        cin.setBorder(null);
        cin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cinMouseClicked(evt);
            }
        });
        cin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cinKeyPressed(evt);
            }
        });
        getContentPane().add(cin);
        cin.setBounds(110, 10, 160, 29);

        Supprimer_Client.setBackground(new java.awt.Color(255, 255, 153));
        Supprimer_Client.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "CIN", "Nom", "Prenom", "N° Tele", "Email", "Situation", "Metier", "Date de Naissance", "N° Enfants"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        Supprimer_Client.setSelectionBackground(new java.awt.Color(0, 153, 204));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cin, org.jdesktop.beansbinding.ObjectProperty.create(), Supprimer_Client, org.jdesktop.beansbinding.BeanProperty.create("selectedElement"));
        bindingGroup.addBinding(binding);

        jScrollPane1.setViewportView(Supprimer_Client);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(0, 57, 884, 470);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt0.png"))); // NOI18N
        getContentPane().add(jLabel4);
        jLabel4.setBounds(100, 1, 230, 50);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Accueil/wallhaven-19788.jpg"))); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(0, 0, 900, 540);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Accueil/wallhaven-19788.jpg"))); // NOI18N
        getContentPane().add(jLabel6);
        jLabel6.setBounds(0, 0, 900, 590);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    
     private void Mettre_a_jour()
    {
            try{
            String msj="SELECT * FROM client";
            ps = conn.prepareStatement(msj);
            rs=ps.executeQuery();
            Supprimer_Client.setModel(DbUtils.resultSetToTableModel(rs));
            
        }
        catch(SQLException e){
            
            JOptionPane.showMessageDialog(null,e);
        }
        
    }
    private void cinMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cinMouseClicked
        cin.setText("");
    }//GEN-LAST:event_cinMouseClicked
    
    
    
    public void Supprimer_Client()
    {
    
    
             String id=cin.getText();
               
               
                      try {
                            String sql0="select * from client where CIN_CLIENT='"+id+"'";
                            String sql2 ="delete from client where CIN_CLIENT='"+id+"'";
                            String sql3="delete from dernier_client where CIN_CLIENT='"+id+"'";
                            String sql4="delete from reservation where CIN_CLIENT='"+id+"'";
                            String sql5="delete from paiement where CIN_CLIENT='"+id+"'";
                            
                            PreparedStatement ps0 = conn.prepareStatement(sql0);
                            PreparedStatement ps3 = conn.prepareStatement(sql3);
                            PreparedStatement ps4 = conn.prepareStatement(sql4);
                            PreparedStatement ps5 = conn.prepareStatement(sql5);
                            
                            rs=ps0.executeQuery();
                            if(rs.next())
                                
                                {
                                      
                                        
                                      PreparedStatement ps2 = conn.prepareStatement(sql2);
                                      if(JOptionPane.showConfirmDialog(null, "Supprimer","Vous etes sur?",JOptionPane.YES_NO_CANCEL_OPTION)==JOptionPane.YES_OPTION){
                                      ps3.executeUpdate();
                                      ps4.executeUpdate();
                                      ps5.executeUpdate();
                                      ps2.executeUpdate();
                                      
                                      JOptionPane.showMessageDialog(null,"Client "+id+" Supprimer");
                                      }
                                   
                                }
                            JOptionPane.showMessageDialog(null,"Ce Client n'existe pas !");
                            
                          } 
                          
                      catch (SQLException e) {
                             JOptionPane.showMessageDialog(null, e.getMessage());
                               }
               
               Mettre_a_jour();
    
    }
    private void jLabel1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseReleased
        
       Supprimer_Client();
        
    }//GEN-LAST:event_jLabel1MouseReleased

    private void jLabel3MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseReleased
        
        
           
        
        try {
            
           
           String sql2 = "delete FROM client ";
           String sql3="delete from dernier_client";
           String sql4="delete from reservation";
           String sql5="delete from paiement";
                            
                            PreparedStatement ps0 = conn.prepareStatement(sql2);
                            PreparedStatement ps3 = conn.prepareStatement(sql3);
                            PreparedStatement ps4 = conn.prepareStatement(sql4);
                            PreparedStatement ps5 = conn.prepareStatement(sql5);
                            
                                      if(JOptionPane.showConfirmDialog(null, "Supprimer","Vous etes sur?",JOptionPane.YES_NO_CANCEL_OPTION)==JOptionPane.YES_OPTION){
                                          ps5.executeUpdate();
                                          ps4.executeUpdate();
                                          ps3.executeUpdate();
                                          ps0.executeUpdate();
                                          JOptionPane.showMessageDialog(null,"Client Supprimer");
                                      }
                                      
                                      
            
            
        } catch (SQLException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }//GEN-LAST:event_jLabel3MouseReleased

    private void jLabel2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseReleased
        
        cin.setText("");
        
        
        
    }//GEN-LAST:event_jLabel2MouseReleased

    private void jLabel1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jLabel1KeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
           Supprimer_Client();
        }
    }//GEN-LAST:event_jLabel1KeyPressed

    private void cinKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cinKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
           Supprimer_Client();
        }
    }//GEN-LAST:event_cinKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Supprimer_Client;
    private javax.swing.JTextField cin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
